home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / headers / random.h < prev    next >
Encoding:
Text File  |  2000-09-28  |  4.1 KB  |  136 lines

  1. /*
  2.     File:        Random.h
  3.  
  4.     Contains:    TRandom is a stackbased utility class for random number generation.
  5.                   TRandom.h contains the header file information for the class.
  6.  
  7.     Written by:     
  8.  
  9.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. // Declare label for this header file
  25. #ifndef _RANDOM_
  26. #define _RANDOM_
  27.  
  28.  
  29. // HEADER FILES
  30. #ifndef _DTSCPLUSLIBRARY_
  31. #include "DTSCPlusLibrary.h"
  32. #endif
  33.  
  34. //    TOOLBOX INCLUDES
  35. #ifndef __TYPES__
  36. #include <Types.h>
  37. #endif
  38.  
  39. #ifndef __OSUTILS__
  40. #include <OSUtils.h>
  41. #endif
  42.  
  43. #ifndef __kQUICKDRAW__
  44. #include <Quickdraw.h>
  45. #endif
  46.  
  47. #ifndef __EVENTS__
  48. #include <Events.h>
  49. #endif
  50.  
  51. //     EXTERNAL FUNCTIONS
  52.  
  53.  
  54. // _________________________________________________________________________________________________________ //
  55. //    TRandom Interface
  56. class TRandom
  57. // The Random number class will produce random numbers within a certain range
  58. // which could be specified. Compared with the Toolbox Random function 
  59. // TRandom is also able to swith between various alternate random algorithms
  60. // and the random generator could be cloned using copy constructors
  61. {
  62. public:
  63.     //    TYPEDEFS AND ENUMS
  64.     enum ERandType
  65.     {
  66.         kMACOS, kSHUFFLE, kPM
  67.     };                                            // selected random number generator
  68.  
  69.     //    CONSTRUCTORS & DESTRUCTORS
  70.     TRandom(ERandType = kMACOS,
  71.             long theSeed = 1,
  72.             unsigned short low = 0,
  73.             unsigned short high = 100);            // default constructor
  74.  
  75.     TRandom(const TRandom&);                    // copy constructor
  76.     TRandom& operator=(const TRandom&);            // assignment operator
  77.     virtual~ TRandom();                            // virtual destructor
  78.  
  79. protected:
  80.     Boolean IRandom(ERandType);                    // initialize needed class information
  81.  
  82. public:
  83.     //  MAIN INTERFACES
  84.     unsigned short Next();                        // get next random number
  85.     TRandom& ShuffleSeed();                        // set seed value
  86.     TRandom& SetRandomGenerator(ERandType);        // select random number algorithm
  87.  
  88.     //    PUBLIC ACCESSORS AND MUTATORS
  89.     long GetSeedValue() const;                    // get current seed value
  90.     TRandom& SetSeedValue(const long theSeed);    // get seed value and return a this pointer to a class
  91.     ERandType GetAlgorithmType() const;            // get currently used algoritm type
  92.  
  93. protected:
  94.     //    RANDOM GENERATOR ALGORITHMS/MEMBER FUNCTIONS
  95.     unsigned short MacRandom();                    // Toolbox Random function
  96.     unsigned short ShuffleRandom();                // shuffle Toolbox random values
  97.     TRandom& InitShuffleRandom();                // initialize the shuffle algorithm
  98.     unsigned short ParkMiller();                // Park&Miller Random generator
  99.  
  100.  
  101. protected:
  102.     //    TYPEDEFS AND ENUMS (INTERNAL IMPLEMENTATION)
  103.     enum eSIZE
  104.     {
  105.         kSHUFFLETABLE = 128, kPM1 = 2836, kPM2 = 16807, k16BIT = 65536, kPM3 = 127773, kMSEED = 1618003398, eBIG = 1000000000, kACM_MAX = 2147483647
  106.     };
  107.  
  108.  
  109.     typedef unsigned short(TRandom::* RandGen)();// ptr to actual selected random routine
  110.  
  111.     //    FIELDS
  112.     RandGen fGenerator;                            // selected random generator function
  113.     ERandType fAlgorithm;                        // selected algorithm
  114.     unsigned short fLow;                        // lower limit of random number
  115.     unsigned short fHigh;                        // higher limit of random number
  116.     unsigned short fRange;                        // max size random number
  117.     long fSeed;                                    // random generator seed
  118.     Boolean fState;                                // current state of object, OK or BAD
  119.     unsigned short fPrevNum;                    // previous random number
  120.     unsigned short fShuffleBuf[kSHUFFLETABLE];    // buffer for the shuffle algorithm
  121. };
  122.  
  123.  
  124. #endif 
  125.  
  126. // _________________________________________________________________________________________________________ //
  127.  
  128.  
  129. /*    Change History (most recent last):
  130.   No        Init.    Date        Comment
  131.   1            khs        6/2/92        New file
  132.   2            khs        1/7/93        Cleanup
  133. */
  134.  
  135.  
  136.